import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '@/views/Home.vue';
import Login from '@/components/Login.vue';
import NotFound from '@/components/NotFound.vue';
import RecordList from '@/views/RecordManage/RecordList.vue';

Vue.use(VueRouter);

const routes = [
  { path: '/', component: Home },
  { path: '/login', component: Login },
  {
    path: '/home',
    name: 'Home',
    redirect: '/recordList',
    component: Home,
    children:
      [
        { path: '/recordList', component: RecordList,
        children: [],
        },
      ],
  },
  {
    path: '*', // 检查路径是否为上面设置的，否则跳到指定页面
    component: NotFound,
  },
];

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes,
});

/*router.beforeEach((to, from, next) => {
  // to将要访问路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next（）放行   next('/login'）强制跳转
  // 如果用户访问的登录页，直接放行

  if (to.path === '/home') return next();

  // 从sessionStorage中获取token
  const tokenStr = window.sessionStorage.getItem('token');

  // 没有token , 强制跳转到登录页
  if (!tokenStr) return next('/Login');
  next();
});*/

export default router;
